<?php
//用户使用日志记录

namespace app\admin\middleware;

use app\admin\model\AdminActionLog as AdminActionLogM;
class AdminActionLog
{
    public function handle($request, \Closure $next)
    {
        $response = $next($request);


        $config = get_config_cache('user_usage_log');
        if(intval($config['is_enabled']) == 1)
        {
            $data = input();
            try {
                //登录 不需要记录用户密码信息
                if($request->action() == 'in'and $request->controller() == 'Login'){
                    $data = [];
                }
                $save_data = [
                    'controller' => $request->controller(),
                    'action' => $request->action(),
                    'create_time' => time(),
                    'user_id' => intval($request->user_id),
                    'data' => json_encode($data, JSON_UNESCAPED_UNICODE),
                ];
                (new AdminActionLogM)->save($save_data);
            } catch (\Exception $e) {
                re_error_json($e->getMessage());
            }
        }
        return $response;
    }
}